﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Hejian_PCBA_Test.Procdure
{
    [XmlRoot("控制板配置")]
    [Serializable]
    internal class ConfigHelper
    {
        public static Config Load()
        {
            CreateSample();
            XmlSerializer serializer = new XmlSerializer(typeof(Config));

            TextReader reader = new StreamReader(path);
            // 初始化默认值

            var obj = serializer.Deserialize(reader);
            reader.Close();

            return obj as Config;
        }

        public static void CreateSample()
        {
            if (!File.Exists(path))
            {
                Config config = new Config
                {
                    RelayPort = "COM2",
                    ConfigRFilePath = "1.csv",
                    ConfigVFilePath = "2.csv",
                    DM3058EPort = "COM1",
                    SNLength = 12,
                    LC10PPort = "COM8",
                    AutoTest = false,
                    MotorSpeed = 4000,
                    NGCount = 0,
                    OKCount = 0,
                    OnlyViewNG = false,
                    TestResistor = true,
                    TestVoltage = false,
                };

                XmlSerializer serializer = new XmlSerializer(typeof(Config));
                TextWriter writer = new StreamWriter(path);
                // 初始化默认值

                serializer.Serialize(writer, config);
                writer.Close();
            }
        }

        private static string path = "config.xml";

        public static void Save(Config config)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Config));
            TextWriter writer = new StreamWriter(path);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
        }
    }
}